<?php

class m120210_063548_create_censor extends CDbMigration {

	protected $_tables = array(
		'censor'=>array(
			'_id'=>'pk',
			'insertTime'=>'integer',
			'updateTime'=>'integer',
			'search'=>'string',
			'replace'=>'string',
		),
	);
	protected $_indexes = array(
		'censor'=>array(
			'search',
		),
	);

	public function safeUp() {
		foreach ($this->_tables as $key=>$value) {
			$this->createTable($key, $value);
		}

		foreach ($this->_indexes as $table=>$columns) {
			foreach ($columns as $column) {
				$name = 'idx_' . $table . '_' . $column;
				$this->createIndex($name, $table, $column);
			}
		}

		$auth = Yii::app()->authManager;

		// Censor Operation
		$auth->createOperation('censorList', 'List censor');
		$auth->createOperation('censorView', 'View censor');
		$auth->createOperation('censorCreate', 'Create censor');
		$auth->createOperation('censorUpdate', 'Update censor');
		$auth->createOperation('censorDelete', 'Delete censor');
		$auth->createOperation('censorManage', 'Manage censor');

		// Censor Moderator
		$role = $auth->createRole('censorModerator', 'Censor Moderator');
		$role->addChild('censorList');
		$role->addChild('censorView');
		$role->addChild('censorCreate');
		$role->addChild('censorUpdate');
		$role->addChild('censorDelete');
		$role->addChild('censorManage');

		$auth->addItemChild('admin', 'censorModerator');
	}

	public function safeDown() {
		foreach ($this->_indexes as $table=>$columns) {
			foreach ($columns as $column) {
				$name = 'idx_' . $table . '_' . $column;
				$this->dropIndex($name, $table, $column);
			}
		}
		
		foreach ($this->_tables as $key=>$value) {
			$this->dropTable($key);
		}

		$auth = Yii::app()->authManager;

		$auth->removeItemChild('admin', 'censorModerator');

		$auth->removeItemChild('censorModerator', 'censorList');
		$auth->removeItemChild('censorModerator', 'censorView');
		$auth->removeItemChild('censorModerator', 'censorCreate');
		$auth->removeItemChild('censorModerator', 'censorUpdate');
		$auth->removeItemChild('censorModerator', 'censorDelete');
		$auth->removeItemChild('censorModerator', 'censorManage');

		$auth->removeAuthItem('censorList');
		$auth->removeAuthItem('censorView');
		$auth->removeAuthItem('censorCreate');
		$auth->removeAuthItem('censorUpdate');
		$auth->removeAuthItem('censorDelete');
		$auth->removeAuthItem('censorManage');

		$auth->removeAuthItem('censorModerator');
	}

}